<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: AddUserAccount.php
 * Date: 2017/2/12
 * Time: 1:08
 */

namespace app\api\model\page\user;

use app\api\model\dao\Circle;
use app\api\model\dao\DashangGift;
use app\api\model\dao\DashangRecord;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteStatistics;
use app\api\model\dao\UpRank;
use app\api\model\dao\UserAccount;
use app\api\model\dao\UserCurrency;
use app\api\model\data\circle\CircleData;
use app\api\model\data\note\RecordNoteData;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;
use think\Validate;


class UserIndex extends PageBase
{

    private $objNote;
    private $objUserAccount;
    private $objDashangRecond;
    private $objRecordNoteData;
    private $objNoteStatistics;
    private $objUpRank;
    private $objCircle;
    private $objUserCurrency;
    private $objNotePraise;
    private $objDashangGift;
    private $objCircleData;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objUserAccount = new UserAccount();
        $this->objDashangRecond = new DashangRecord();
        $this->objRecordNoteData = new RecordNoteData();
        $this->objNoteStatistics = new NoteStatistics();
        $this->objUpRank = new UpRank();
        $this->objCircle = new Circle();
        $this->objUserCurrency = new UserCurrency();
        $this->objNotePraise = new NotePraise();
        $this->objDashangGift = new DashangGift();
        $this->objCircleData = new CircleData();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'user_id' => 'require',
        ];
        $msg = [
            'user_id.require' => '请选择用户',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        // 1、获取用户信息
        $user_account = $this->objUserAccount->getUserAccount(['user_id' => $param['user_id']]);
        if (false === $user_account) {
            Log::error(__METHOD__ . 'get user account from db fail. [user_id: ' . $param['user_id'] . ']');
            return $this->getRes(Error::ERR_SYS);
        }
        if (empty($user_account)) {
            return $this->getRes(Error::ERR_USER_NOT_EXIST);
        }
        $user_account = $user_account[0];

        $user_extend_info = [];
        if (!empty($user_account['extend_info'])) {
            $user_extend_info = json_decode($user_account['extend_info'], true);
        }

        $constellation = '未知';
        if (!empty($user_extend_info['constellation'])) {
            $constellation = $user_extend_info['constellation'];
        }
        $location = '未知';
        if (!empty($user_extend_info['location'])) {
            $location = explode(' ', $user_extend_info['location']);
            $location = $location[0];
        }
        $dating_intention = '未知';
        if (!empty($user_extend_info['dating_intention'])) {
            $dating_intention = $user_extend_info['dating_intention'];
        }

        $this->data['user'] = [
            'name' => $user_account['nick_name'],
            'photo' => $user_account['avatar'],
            'sex' => 0 == $user_account['sex'] ? 'M' : 'F',
            'briefing' => '我是一个个人简介',
            'age' => strval($user_account['age']),
            'constellation' => $constellation,
            'local' => $location,
            'dating_intention' => $dating_intention,
        ];

        // 2、获取 社区等级、漫豆、赞
        $user_bean = 0;
        $user_currency = $this->objUserCurrency->getCurrencyByUserid($param['user_id'], Flag::CURENCY_TYPE_INTEGRAL);
        if (false === $user_currency) {
            Log::error(__METHOD__ . ' get user currency fail.[userid:' . $param['user_id'] . ']');
        } else {
            $user_currency = $this->modelToArray($user_currency);
            $user_bean = $user_currency['amount'];
        }
        $arrCond = [
            'note_userid' => ['=', $param['user_id']],
            'is_delete' => ['=', Flag::CONST_NOTEPRIASE_DELETE_NOT],
        ];
        $praise_count = $this->objNotePraise->getNotePraiseCountByConds($arrCond);
        if (false === $praise_count) {
            Log::error(__METHOD__ . ' get note praise count fail.[conds:' . json_encode($arrCond) . ']');
            $praise_count = 0;
        }
        $this->data['community'] = [
            'level' => Common::conversionLevel($user_bean),
            'user_bean' => $user_bean,
            'praise' => $praise_count,
        ];

        $this->data['profile'] = [];
        if (!empty($user_extend_info['constellation'])) {
            $this->data['profile'][] = $user_extend_info['constellation'];
        }
        if (!empty($user_account['age'])) {
            $this->data['profile'][] = $user_account['age'] . '岁';
        }
        if (!empty($user_extend_info['home'])) {
            $home = explode(' ', $user_extend_info['home']);
            $this->data['profile'][] = $home[0];
        }
        if (!empty($user_extend_info['sexual_orientation'])) {
            $this->data['profile'][] = $user_extend_info['sexual_orientation'];
        }
        if (!empty($user_extend_info['marriage_status'])) {
            $this->data['profile'][] = $user_extend_info['marriage_status'];
        }
        if (!empty($user_extend_info['dating_intention'])) {
            $this->data['profile'][] = '交友意向' . $user_extend_info['dating_intention'];
        }
        $this->data['profile'] = array_slice($this->data['profile'], 0, 4);

        // 3、收到的礼物
        $arrCond = [
            'note_userid' => ['=', $param['user_id']]
        ];
        $dashang_recond = $this->objDashangRecond->getDashangRecord($arrCond, ['gift_id']);
        if (false === $dashang_recond) {
            Log::error(__METHOD__ . ' get dashang_record fail.[conds:' . json_encode($arrCond) . ']');
            $dashang_recond = [];
        }
        $gift_id = [];
        $giftid2Gift = [];
        foreach ($dashang_recond as $val) {
            $gift_id[] = $val['gift_id'];
        }
        if (!empty($gift_id)) {
            $dashang_gift = $this->objDashangGift->getDashangGiftByGiftid($gift_id);
            if (false === $dashang_gift) {
                Log::error(__METHOD__ . ' get dashang_gift fail.[conds:' . json_encode($gift_id) . ']');
            } else {
                foreach ($dashang_gift as $val) {
                    $giftid2Gift[$val['id']] = $val['pic'];
                }
            }
        }
        $this->data['reward_gift'] = array_values($giftid2Gift);

        // 4、关注的圈子
        $this->data['circle'] = $this->objCircleData->followCircle($param['user_id'], 0, 4);

        return $this->getRes();
    }
}
